package com.wannengbang.cloudleader.shop;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.gyf.immersionbar.ImmersionBar;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.BaseResponseBean;
import com.wannengbang.cloudleader.bean.ShopOrderDetailsBean;
import com.wannengbang.cloudleader.bean.ShopOrderListBean;
import com.wannengbang.cloudleader.event.OrderRefushEvent;
import com.wannengbang.cloudleader.homepage.PayOrderActivity;
import com.wannengbang.cloudleader.shop.adapter.ShopOrderProductListAdapter;
import com.wannengbang.cloudleader.shop.model.IShopModel;
import com.wannengbang.cloudleader.shop.model.ShopModelImpl;
import com.wannengbang.cloudleader.utils.DateTimeUtil;
import com.wannengbang.cloudleader.utils.NumberFormatUtils;
import com.wannengbang.cloudleader.utils.ToastUtil;
import com.wannengbang.cloudleader.widget.AppTitleBar;
import com.wannengbang.cloudleader.widget.CommonNoTitleDialog;
import com.wannengbang.cloudleader.widget.ViewLoading;
import com.wannengbang.cloudleader.widget.WrapContentLinearLayoutManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopOrderDetailsActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_order_status)
    TextView tvOrderStatus;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_mobile)
    TextView tvMobile;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_copy_address)
    TextView tvCopyAddress;
    @BindView(R.id.ll_select_address)
    LinearLayout llSelectAddress;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_total_price)
    TextView tvTotalPrice;
    @BindView(R.id.tv_order_no)
    TextView tvOrderNo;
    @BindView(R.id.tv_copy)
    TextView tvCopy;
    @BindView(R.id.tv_create_time)
    TextView tvCreateTime;
    @BindView(R.id.tv_delivery_type)
    TextView tvDeliveryType;
    @BindView(R.id.ll_delivery_type)
    LinearLayout llDeliveryType;
    @BindView(R.id.tv_delivery_no)
    TextView tvDeliveryNo;
    @BindView(R.id.tv_copy_no)
    TextView tvCopyNo;
    @BindView(R.id.ll_delivery_no)
    LinearLayout llDeliveryNo;
    @BindView(R.id.tv_btn_sign)
    TextView tvBtnSign;
    @BindView(R.id.tv_btn_pay)
    TextView tvBtnPay;
    @BindView(R.id.ll_btn)
    LinearLayout llBtn;
    @BindView(R.id.tv_freight_price)
    TextView tvFreightPrice;
    private ShopOrderListBean.DataBean.ItemListBean dataBean;
    private IShopModel shopModel;
    private CommonNoTitleDialog.Builder dialogBuild;
    private String shop_order_id;
    private String[] deliverName = {"顺丰速运", "圆通速递", "中通快递", "申通快递", "韵达速递", "邮政包裹", "德邦快递", "百世快递", "天天快递"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_order_details);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarDarkFont(false)
                .navigationBarDarkIcon(true)
                .navigationBarColor(R.color.white)
                .statusBarDarkFont(false, 0.2f)
                .init();
        registerEventBus();
        initView();
        requestOrderShow();
    }

    @Subscribe
    public void updata(OrderRefushEvent event) {
        requestOrderShow();
    }

    public void initView() {
        shopModel = new ShopModelImpl();
        shop_order_id = getIntent().getStringExtra("shop_order_id");
    }


    public void requestOrderShow() {
        ViewLoading.showProgress(mActivity);
        shopModel.requestOrderShow(shop_order_id, new DataCallBack<ShopOrderDetailsBean>() {
            @Override
            public void onSuccessful(ShopOrderDetailsBean bean) {
                dataBean = bean.getData();
                if (dataBean != null) {
                    tvName.setText(bean.getData().getRecv_name());
                    tvMobile.setText(bean.getData().getMobile());
                    tvAddress.setText(bean.getData().getProvince() + bean.getData().getCity() + bean.getData().getArea() + bean.getData().getAddress());

                    tvBtnPay.setVisibility(View.GONE);
                    tvBtnSign.setVisibility(View.GONE);
                    llBtn.setVisibility(View.GONE);
                    if (dataBean.getStatus() == 1) {
                        tvOrderStatus.setText("待付款");
                        tvBtnPay.setVisibility(View.VISIBLE);
                        llBtn.setVisibility(View.VISIBLE);
                    } else if (dataBean.getStatus() == 2) {
                        tvOrderStatus.setText("待发货");
                    } else if (dataBean.getStatus() == 3) {
                        tvOrderStatus.setText("待收货");
                        tvBtnSign.setVisibility(View.VISIBLE);
                        llBtn.setVisibility(View.VISIBLE);
                    } else if (dataBean.getStatus() == 4) {
                        tvOrderStatus.setText("已完成");
                    } else if (dataBean.getStatus() == 5) {
                        tvOrderStatus.setText("申请取消中");
                    } else if (dataBean.getStatus() == 6) {
                        tvOrderStatus.setText("已取消");
                    }

                    tvTotalPrice.setText(dataBean.getIntegral_price());
                    String freight = NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getShop_order_goods().get(0).getFreight()) / 100);

                    tvFreightPrice.setText(freight);

                    ShopOrderProductListAdapter listAdapter = new ShopOrderProductListAdapter(dataBean.getShop_order_goods());
                    recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(mActivity));
                    recyclerView.setAdapter(listAdapter);

                    tvCreateTime.setText(DateTimeUtil.format7(new Date(Long.parseLong(dataBean.getCreate_time()) * 1000)));
                    tvOrderNo.setText(dataBean.getId() + "");

                    if ((dataBean.getExpress_type() > 0 && dataBean.getExpress_type() < 10) && !TextUtils.isEmpty(dataBean.getExpress_no())) {
                        llDeliveryType.setVisibility(View.VISIBLE);
                        llDeliveryNo.setVisibility(View.VISIBLE);
                        tvDeliveryType.setText(deliverName[dataBean.getExpress_type() - 1]);
                        tvDeliveryNo.setText(dataBean.getExpress_no());
                    } else {
                        llDeliveryType.setVisibility(View.GONE);
                        llDeliveryNo.setVisibility(View.GONE);
                    }
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    public void requestStoreSign(String shop_order_id) {
        ViewLoading.showProgress(mActivity, "签收中......");
        shopModel.requestStoreSign(shop_order_id, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort(bean.getMsg());
                EventBus.getDefault().post(new OrderRefushEvent());
                requestOrderShow();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick({R.id.tv_copy_address, R.id.tv_copy, R.id.tv_btn_sign, R.id.tv_btn_pay, R.id.tv_copy_no})
    public void onViewClicked(View view) {
        if (dataBean == null) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_copy:
                //获取剪贴板管理器
                ClipboardManager cm = (ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                ClipData mClipData = ClipData.newPlainText("Label", dataBean.getId() + "");
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                ToastUtil.showShort("复制成功");
                break;
            case R.id.tv_copy_no:
                //获取剪贴板管理器
                cm = (ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                mClipData = ClipData.newPlainText("Label", dataBean.getExpress_no());
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                ToastUtil.showShort("复制成功");
                break;
            case R.id.tv_copy_address:
                if (dataBean == null) {
                    return;
                }
                String address = dataBean.getRecv_name() + " " + dataBean.getMobile() + " " + dataBean.getProvince() + dataBean.getCity() + dataBean.getArea() + dataBean.getAddress();
                //获取剪贴板管理器
                cm = (ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                mClipData = ClipData.newPlainText("Label", address);
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                ToastUtil.showShort("复制成功");
                break;
            case R.id.tv_btn_sign:
                dialogBuild = new CommonNoTitleDialog.Builder(mActivity);
                dialogBuild.setMessage("确认收货?");
                dialogBuild.setPositiveButton("确定", v -> {
                    dialogBuild.dismiss();
                    requestStoreSign(dataBean.getId() + "");
                });
                dialogBuild.setNegativeButton("取消", v -> dialogBuild.dismiss());
                dialogBuild.setCancelable(false);
                dialogBuild.show();
                break;
            case R.id.tv_btn_pay:
                String order_no = dataBean.getId() + "";
                String totalPrice = NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getFreight()) / 100);
                Intent intent = new Intent(mActivity, ShopPayOrderActivity.class);
                intent.putExtra("order_no", order_no);
                intent.putExtra("totalPrice", totalPrice);
                startActivity(intent);
                break;
        }
    }
}
